package com.wpen.unit.infra.repository.job.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.BatchExecLogMapper;
import com.wpen.unit.infra.mybatis.job.po.BatchExecLogPo;
import com.wpen.unit.infra.mybatis.job.po.BatchExecLogPo.BatchExecLogColumns;
import com.wpen.unit.infra.mybatis.job.po.JobUndoPlanPo.JobUndoPlanColumns;
import com.wpen.unit.infra.repository.job.BatchExecLogRepository;
import com.wpen.unit.utils.StringUtil;

/**
 * 批次执行日志维护.
 * 
 * @author pg
 *
 */
@Repository
@Transactional
public class BatchExecLogRepositoryImpl extends ServiceImpl<BatchExecLogMapper, BatchExecLogPo>
		implements BatchExecLogRepository {

	@Override
	public BatchExecLogPo queryByIdWithLock(Serializable id) {
		QueryWrapper<BatchExecLogPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(JobUndoPlanColumns.ID.name(), id);
		queryWrapper.last(" FOR UPDATE");
		return super.getOne(queryWrapper);
	}

	@Override
	public long count(BatchExecLogPo batchExecLogPo) {
		QueryWrapper<BatchExecLogPo> queryWrapper = createQuery(batchExecLogPo);
		return super.count(queryWrapper);
	}

	@Override
	public List<BatchExecLogPo> queryList(int index, int size, BatchExecLogPo batchExecLogPo) {
		QueryWrapper<BatchExecLogPo> queryWrapper = createQuery(batchExecLogPo);
		queryWrapper.orderByDesc(BatchExecLogColumns.BATCH_TRIGGER_TIME.name());
		if (size > 0) {
			Page<BatchExecLogPo> page = new Page<>(index, size, false);
			return super.page(page, queryWrapper).getRecords();
		}
		// queryWrapper.orderBy(true, false, BatchInfoPoColumns.ID.name());
		return getBaseMapper().selectList(queryWrapper);
	}

	private QueryWrapper<BatchExecLogPo> createQuery(BatchExecLogPo batchExecLogPo) {
		QueryWrapper<BatchExecLogPo> queryWrapper = Wrappers.query();
		if (batchExecLogPo.getId() != null) {
			queryWrapper.eq(BatchExecLogColumns.ID.name(), batchExecLogPo.getId());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getBizCode())) {
			queryWrapper.eq(BatchExecLogColumns.BIZ_CODE.name(), batchExecLogPo.getBizCode());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getBatchCode())) {
			queryWrapper.eq(BatchExecLogColumns.BATCH_CODE.name(), batchExecLogPo.getBatchCode());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getBatchType())) {
			queryWrapper.eq(BatchExecLogColumns.BATCH_TYPE.name(), batchExecLogPo.getBatchType());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getScheduleType())) {
			queryWrapper.eq(BatchExecLogColumns.SCHEDULE_TYPE.name(), batchExecLogPo.getScheduleType());
		}
		if (batchExecLogPo.getExecDate() != null) {
			queryWrapper.eq(BatchExecLogColumns.EXEC_DATE.name(), batchExecLogPo.getExecDate());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getExecStatus())) {
			queryWrapper.eq(BatchExecLogColumns.EXEC_STATUS.name(), batchExecLogPo.getExecStatus());
		}
		if (batchExecLogPo.getStartupTime() != null) {
			queryWrapper.eq(BatchExecLogColumns.STARTUP_TIME.name(), batchExecLogPo.getStartupTime());
		}
		// if (batchExecLogPo.getEndTime() != null) {
		// queryWrapper.eq(BatchExecLogColumns.END_TIME.name(),
		// batchExecLogPo.getEndTime());
		// }
		return queryWrapper;
	}

	@Override
	public BatchExecLogPo queryFirst(BatchExecLogPo batchExecLogPo) {
		QueryWrapper<BatchExecLogPo> queryWrapper = Wrappers.query();
		if (batchExecLogPo.getId() != null) {
			queryWrapper.eq(BatchExecLogColumns.ID.name(), batchExecLogPo.getId());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getBizCode())) {
			queryWrapper.eq(BatchExecLogColumns.BIZ_CODE.name(), batchExecLogPo.getBizCode());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getBatchCode())) {
			queryWrapper.eq(BatchExecLogColumns.BATCH_CODE.name(), batchExecLogPo.getBatchCode());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getBatchType())) {
			queryWrapper.eq(BatchExecLogColumns.BATCH_TYPE.name(), batchExecLogPo.getBatchType());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getScheduleType())) {
			queryWrapper.eq(BatchExecLogColumns.SCHEDULE_TYPE.name(), batchExecLogPo.getScheduleType());
		}
		if (batchExecLogPo.getExecDate() != null) {
			queryWrapper.eq(BatchExecLogColumns.EXEC_DATE.name(), batchExecLogPo.getExecDate());
		}
		if (StringUtil.isNotEmpty(batchExecLogPo.getExecStatus())) {
			queryWrapper.eq(BatchExecLogColumns.EXEC_STATUS.name(), batchExecLogPo.getExecStatus());
		}
		if (batchExecLogPo.getStartupTime() != null) {
			queryWrapper.eq(BatchExecLogColumns.STARTUP_TIME.name(), batchExecLogPo.getStartupTime());
		}
		// if (batchExecLogPo.getEndTime() != null) {
		// queryWrapper.eq(BatchExecLogColumns.END_TIME.name(),
		// batchExecLogPo.getEndTime());
		// }
		// queryWrapper.orderBy(true, false, BatchExecLogPoColumns.ID.name());
		return super.getOne(queryWrapper);
	}

	@Override
	public BatchExecLogPo queryById(Serializable id) {
		return super.getById(id);
	}

	@Override
	public boolean insert(BatchExecLogPo batchExecLogPo) {
//        batchExecLogPo.setVersion(1);
//        batchExecLogPo.setTimeStamp(new java.sql.Timestamp(System.currentTimeMillis()));
		return super.save(batchExecLogPo);
	}

	@Override
	public boolean insertBatch(List<BatchExecLogPo> listBatchExecLogPos) {
		int rs = getBaseMapper().insertBatchSomeColumn(listBatchExecLogPos);
		return SqlHelper.retBool(rs);
	}

	@Override
	public boolean updateById(BatchExecLogPo batchExecLogPo) {
		UpdateWrapper<BatchExecLogPo> updateWrapper = Wrappers.update();
		updateWrapper.eq(BatchExecLogColumns.ID.name(), batchExecLogPo.getId());
//        updateWrapper.eq(BatchExecLogColumns.VERSION.name(), batchExecLogPo.getVersion());
//        batchExecLogPo.setVersion(batchExecLogPo.getVersion() + 1);
		boolean rs = super.update(batchExecLogPo, updateWrapper);
		// 根据ID更新所有字段，空值也更新
		// int num = getBaseMapper().alwaysUpdateSomeColumnById(BatchInfoPo);
		// boolean rs = SqlHelper.retBool(num);
		if (!rs) {
			throw new UpdateZeroException();
		}
		return rs;
	}

	@Override
	public boolean updateByIdWithNull(BatchExecLogPo batchExecLogPo) {
		// 根据ID更新所有字段，空值也更新
		int num = getBaseMapper().alwaysUpdateSomeColumnById(batchExecLogPo);
		boolean rs = SqlHelper.retBool(num);
		if (!rs) {
			throw new UpdateZeroException();
		}
		return rs;
	}

	@Override
	public boolean deleteByIds(Serializable[] ids) {
		UpdateWrapper<BatchExecLogPo> updateWrapper = Wrappers.update();
		updateWrapper.in(BatchExecLogColumns.ID.name(), (Object[]) ids);
		return super.remove(updateWrapper);
	}

//    public BatchExecLogPo queryByExample(BatchExecLogPo batchExecLogPo) {
//        QueryWrapper<BatchExecLogPo> queryWrapper = Wrappers.query();
//        if (batchExecLogPo.getId() != null) {
//            queryWrapper.eq(BatchExecLogColumns.ID.name(), batchExecLogPo.getId());
//        }
//        if (StringUtil.isNotEmpty(batchExecLogPo.getBizCode())) {
//            queryWrapper.eq(BatchExecLogColumns.BIZ_CODE.name(), batchExecLogPo.getBizCode());
//        }
//        if (StringUtil.isNotEmpty(batchExecLogPo.getBatchCode())) {
//            queryWrapper.eq(BatchExecLogColumns.BATCH_CODE.name(), batchExecLogPo.getBatchCode());
//        }
//        if (StringUtil.isNotEmpty(batchExecLogPo.getBatchType())) {
//            queryWrapper.eq(BatchExecLogColumns.BATCH_TYPE.name(), batchExecLogPo.getBatchType());
//        }
//        if (StringUtil.isNotEmpty(batchExecLogPo.getScheduleType())) {
//            queryWrapper.eq(BatchExecLogColumns.SCHEDULE_TYPE.name(), batchExecLogPo.getScheduleType());
//        }
//        if (batchExecLogPo.getExecDate() != null) {
//            queryWrapper.eq(BatchExecLogColumns.EXEC_DATE.name(), batchExecLogPo.getExecDate());
//        }
//        if (StringUtil.isNotEmpty(batchExecLogPo.getExecResult())) {
//            queryWrapper.eq(BatchExecLogColumns.EXEC_RESULT.name(), batchExecLogPo.getExecResult());
//        }
//        if (batchExecLogPo.getStartupTime() != null) {
//            queryWrapper.eq(BatchExecLogColumns.STARTUP_TIME.name(), batchExecLogPo.getStartupTime());
//        }
//        if (batchExecLogPo.getEndTime() != null) {
//            queryWrapper.eq(BatchExecLogColumns.END_TIME.name(), batchExecLogPo.getEndTime());
//        }
//		Page<BatchExecLogPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, BatchExecLogPoColumns.ID.name());
//    }

}
